﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Windows.Forms;
using lib;

namespace DocClient
{
    class DocThread
    {
        private DoctorForm _form;
        private TcpClient _serverConnection;
        private BinaryFormatter formatter = new BinaryFormatter();
        SecureConnectionClient secureConnection;
        private string results;

        public void Handle(object o)
        {
            _form = (DoctorForm)o;
            _serverConnection = _form.serverConnection;
            secureConnection = _form.secureConnection;
            TestResult t;
            object obj;
            string[] serverstring;
            while (true)
            {


                
                //String[] serverstring = (formatter.Deserialize(_serverConnection.GetStream()) as String).Split(':');
                serverstring = secureConnection.ReadObjectAsString().Split(':');
                if (serverstring[0].Equals("measurement"))
                {
                    //serverstring[1]
                        //MeasuredValues naam = formatter.Deserialize(_serverConnection.GetStream()) as MeasuredValues;
                    MeasuredValues naam = secureConnection.ReadObject() as MeasuredValues;
                }
                if (serverstring[0].Equals("test"))
                {
                    try
                    {
                        //TestResult naam = new TestResult(serverstring[1]);

                        results = serverstring[1];
                        Thread graphThread = new Thread(createThread);
                        graphThread.Start();
                    }
                    catch { }


                }
                if (serverstring[0].Equals("handlefullmeasurements"))
                {
                    MeasuredValues naam = new MeasuredValues(serverstring[2]);
                    string[] hulp = serverstring[2].Split(' ');
                    

                    System.Diagnostics.Debug.WriteLine("THIS IS IMPORTANT!!!");
                    System.Diagnostics.Debug.WriteLine(serverstring[2]);
                    _form.SetRNaam(serverstring[1]);
                    _form.SetRHeartBeat(Convert.ToString(naam.heartBeat) + " Bpm");
                    _form.SetRSpeed(Convert.ToString((float)naam.speed/10) + " KM/h");
                    _form.SetRPower(Convert.ToString(naam.power) + " Watt");
                    _form.SetRTime(Convert.ToString(hulp[7].Substring(1,2) +":"+ hulp[7].Substring(3,2)));
                    System.Diagnostics.Debug.WriteLine(naam.time.ToString());
                    _form.SetRrpm(Convert.ToString(naam.rpm) + " RPM");
                    _form.SetRDesiredPower(Convert.ToString(naam.currentPower) + " Watt");
                    _form.SetREnergy(Convert.ToString(naam.energy) + " KJ");
                    _form.SetDistance(Convert.ToString(naam.distance/10) + " Km");
                }
                else if (serverstring[0].Equals("dataPacketWithoutThread"))
                {
                    results = serverstring[1];
                    GrafiekManager.getInstance().handleData(results);
                }

                else if (serverstring[0].Equals("dataPacket"))
                {
                    results = serverstring[1];
                    Thread graphThread = new Thread(createThread);
                    graphThread.Start();
                }

                else if (serverstring[0].Equals("singlemeasurement"))
                {
                    String timestamp = /*serverstring[1] +":"+*/ serverstring[2] + ":" + serverstring[3];
                    _form.SetTime(timestamp+":"+serverstring[4]);
                    _form.SetpatientHearthbeat(serverstring[5] + ":" + serverstring[4]);
                    _form.SetpatientPower(serverstring[6] + ":" + serverstring[4]);
                    //_form.SetSingleMeasurement(serverstring[1], serverstring[2], serverstring[3], serverstring[4]);
                }
                else if (serverstring[0].Equals("newpatient"))
                {
                    _form.InitPatient(serverstring[1] + ":" + serverstring[2]);
                    _form.SetPatientnaam(serverstring[1]);
                }
                else if (serverstring[0].Equals("removepatient"))
                {
                    _form.RemovePatient(serverstring[1]);
                }
                else if (serverstring[0].Equals("chat"))
                {
                    try
                    {
                        string message = serverstring[1] + ":" + serverstring[2];
                        _form.OutputTextBoxMessage(message);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Something went wrong when reveiving a chatmessage from the server!");
                    }
                }
                else if (serverstring[0].Equals("savedFilesList"))
                {
                    string[] fileNames = serverstring[1].Split('$');
                    try
                    {
                        _form.sf.RefreshList(fileNames);
                    }
                    catch (Exception)
                    {
                        System.Diagnostics.Debug.WriteLine("EXCEPTION: SelectFileForm == null");
                    }
                }
                else if (serverstring[0].Equals("returnSavedFile"))
                {
                    //ONTVANG TESTRESULT???
                }

            }  
        }

        private void createThreadWithData(TestResult t, DoctorForm _form)
        {
            Application.Run(new Grafieken(t,_form));
        }
        public void createThread()
        {
            Application.Run(new Grafieken(results, _form));
        }
    }
}
